package com.one.group.admin.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.processor.util.StrUtil;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.CustomerQueryDto;
import com.one.group.model.dto.UpdateCustomerDto;
import com.one.group.model.entity.Customer;
import com.one.group.result.Result;
import com.one.group.service.CustomerService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("customer")
@Tag(name = "客户管理接口")
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @PostMapping("list")
    @Operation(summary = "查询客户列表")
    public Result list(@RequestBody CustomerQueryDto customerQueryDto){
//        1.准备分页条件
        Page<Customer> page = new Page<>(customerQueryDto.getPage(), customerQueryDto.getLimit());
//        2.链式编程准备查询条件
        QueryWrapper queryWrapper = new QueryWrapper();

// 2. 构建查询条件
        String query= customerQueryDto.getQuery();
        if (!StrUtil.isBlank(query)) {
            queryWrapper.where(
                    QueryMethods.column(Customer::getName).like(query)
                            .or(QueryMethods.column(Customer::getPhone).like(query))
            );
        }
//        3.查询，返回分页数据
        Page<Customer> customerPage = customerService.page(page, queryWrapper);
        return ResultUtil.success(customerPage);
    }

    @GetMapping("/find")
    @Operation(summary = "根据id查询客户信息")
    public Result find(@RequestParam Long id){
        return ResultUtil.success(customerService.getById(id));
    }
    @GetMapping("findAll")
    @Operation(summary = "查询所有客户信息")
    public Result findAll() {
        return ResultUtil.success(customerService.list());
    }
    @GetMapping("/delete")
    @Operation(summary = "删除客户信息")
    public Result delete(@RequestParam Long id){
        if (customerService.removeById(id))
            return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/add")
    @Operation(summary = "添加客户信息")
    public Result add(@RequestBody UpdateCustomerDto updateCustomerDto){
        Customer customer = new Customer();
        BeanUtils.copyProperties(updateCustomerDto,customer);
        try {
            customer.setAdminId(Long.parseLong(StpUtil.getLoginId().toString()));
        } catch (NumberFormatException e) {
            return ResultUtil.error(ErrorCode.OPERATION_ERROR);
        }
        if (customerService.save(customer)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/update")
    @Operation(summary = "修改客户信息")
    public Result update(@RequestBody UpdateCustomerDto updateCustomerDto){
        Customer customer = new Customer();
        BeanUtils.copyProperties(updateCustomerDto,customer);
        // 安全获取并转换登录ID

        if (customerService.updateById(customer)) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }
    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除客户信息")
    public Result deleteBatch(@RequestBody BaseDeleteDto baseDeleteDto){
        if (customerService.removeByIds(baseDeleteDto.getIds())) return ResultUtil.success();
        else return ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

}
